package xyz.shenmegui.foodcheckserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import xyz.shenmegui.foodcheckserver.DO.Collection;
import xyz.shenmegui.foodcheckserver.DO.Food;
import xyz.shenmegui.foodcheckserver.DO.Menu;
import xyz.shenmegui.foodcheckserver.VO.CollectionList;
import xyz.shenmegui.foodcheckserver.mapper.CollectionMapper;
import xyz.shenmegui.foodcheckserver.mapper.MenuMapper;
import xyz.shenmegui.foodcheckserver.service.CollectionService;
import xyz.shenmegui.foodcheckserver.service.FoodService;
import xyz.shenmegui.foodcheckserver.service.MenuService;
import xyz.shenmegui.foodcheckserver.utils.ObjectUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private MenuService menuService;

    @Autowired
    private FoodService foodService;

    @Override
    public CollectionList getCollectionByUserId(int userId) {
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collection::getUserId, userId);
        queryWrapper.orderByDesc(Collection::getId);
        List<Collection> collections = collectionMapper.selectList(queryWrapper);
        return buildCollectionList(collections);
    }

    @Override
    public int deleteCollection(Collection collection) {
        LambdaUpdateWrapper<Collection> updateWrapper = new LambdaUpdateWrapper<>();
        if (collection.getMenuId() != null) {
            updateWrapper.eq(Collection::getMenuId, collection.getMenuId());
        }
        if (collection.getFoodId() != null) {
            updateWrapper.eq(Collection::getFoodId, collection.getFoodId());
        }

        return collectionMapper.delete(updateWrapper);
    }

    @Override
    public int addCollection(Collection collection) {
        return collectionMapper.insert(collection);
    }

    private CollectionList buildCollectionList(List<Collection> collectionList) {
        CollectionList result = new CollectionList();
        if (!CollectionUtils.isEmpty(collectionList)) {
            List<Integer> foodIdList = collectionList.stream().filter(collection -> {
                return ObjectUtil.isNotNull(collection.getFoodId()) && collection.getFoodId() > 0;
            }).map(Collection::getFoodId).collect(Collectors.toList());
            List<Integer> menuIdList = collectionList.stream().filter(collection -> {
                return ObjectUtil.isNotNull(collection.getMenuId()) && collection.getMenuId() > 0;
            }).map(Collection::getMenuId).collect(Collectors.toList());
            List<Integer> seedIdList = collectionList.stream().filter(collection -> {
                return ObjectUtil.isNotNull(collection.getSeedId()) && collection.getSeedId() > 0;
            }).map(Collection::getSeedId).collect(Collectors.toList());
            ArrayList<Food> foods = new ArrayList<>();
            ArrayList<Menu> menus = new ArrayList<>();
            if (!CollectionUtils.isEmpty(foodIdList)) {
                for (Integer id : foodIdList) {
                    Food food = foodService.getFoodById(id);
                    foods.add(food);
                }
            }
            if (!CollectionUtils.isEmpty(menuIdList)) {
                for (Integer id : menuIdList) {
                    Menu menu = menuService.getMenuById(id);
                    if (ObjectUtil.isNotNull(menu)) {
                        menus.add(menu);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(seedIdList)) {
                for (Integer id : seedIdList) {
                    List<Menu> menuList = menuService.getMenuBySeedId(id);
                    if (!CollectionUtils.isEmpty(menuList)) {
                        menus.addAll(menuList);
                    }
                }
            }
            result.setFoodList(foods);
            result.setMenuList(menus);
        }
        return result;
    }
}
